var DataPrx = DataPrx || {};

module.exports = DataPrx;

DataPrx.config = {
  "host": "172.20.91.211",
  "user": "taf",
  "password": "taf2015",
  "database": "db_qcjk"
}

DataPrx.mysql = () => {
  var pool = require('mysql').createPool({
        host: DataPrx.config.host,
        user: DataPrx.config.user,
        password: DataPrx.config.password,
        database: DataPrx.config.database,
        dateStrings: true
    })

  return sql => new Promise((resolve, reject) => {
    pool.getConnection((err, conn) => {
      if (err) return reject(err)
      conn.query(sql, (err, result) => {
        conn.release()
        err ? reject(err) : resolve(result)
      })
    })
  })
}

var obj = null;
DataPrx.knex = function() {
  if (!obj) {
      obj = require('knex')({
      client: 'mysql',
      connection: {
            host: DataPrx.config.host,
            user: DataPrx.config.user,
            password: DataPrx.config.password,
            database: DataPrx.config.database,
            dateStrings: true
        }
    });
  }
  
  return obj;
}
